package market

import (
	"context"
	"errors"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-oppty-service/proto/micro_gfyx_oppty_service_proto/micro-gfyx-oppty-service/market"
	"git.mycaigou.com/gfyx/micro-gfyx-oppty-service/service/market/outbound_task"
	"git.myscrm.cn/golang/common/ykerrcode"
	"git.myscrm.cn/golang/stark/v4"
)

type OutboundTaskServer struct {
	outboundTaskService outbound_task.OutboundTaskServiceIface
}

func NewOutboundTaskServer(
	outboundTaskService outbound_task.OutboundTaskServiceIface,
) pb.OutboundTaskServiceServer {
	return &OutboundTaskServer{
		outboundTaskService: outboundTaskService,
	}
}

// CreateTask 创建任务
func (s *OutboundTaskServer) CreateTask(ctx context.Context, request *pb.CreateTaskRequest) (*pb.CreateTaskResponse, error) {
	resp, err := s.outboundTaskService.CreateTask(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc SaveTask request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc SaveTask request:%+v, succeed", request)
	return resp, nil
}

// UpdateTask 更新任务
func (s *OutboundTaskServer) UpdateTask(ctx context.Context, request *pb.UpdateTaskRequest) (*pb.UpdateTaskResponse, error) {
	resp, err := s.outboundTaskService.UpdateTask(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc UpdateTask request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc UpdateTask request:%+v, succeed", request)
	return resp, nil
}

// 修改客户意向
func (s *OutboundTaskServer) UpdateIntention(ctx context.Context, request *pb.UpdateIntentionRequest) (*pb.UpdateIntentionResponse, error) {
	resp := &pb.UpdateIntentionResponse{}
	err := s.outboundTaskService.UpdateIntention(ctx, request)
	resp.IsSucceed = true
	if err != nil {
		resp.IsSucceed = false
	}
	return resp, err
}

func (s *OutboundTaskServer) SetTaskLogRead(ctx context.Context, request *pb.SetTaskLogReadRequest) (*pb.SetTaskLogReadResponse, error) {
	resp := &pb.SetTaskLogReadResponse{}
	if request.CallInstanceId <= 0 {
		return resp, ykerrcode.TogRPCError(ykerrcode.INVALID_PARAMS, "缺少必填参数")
	}
	err := s.outboundTaskService.SetTaskLogRead(ctx, request.CallInstanceId)
	resp.IsSucceed = true
	if err != nil {
		resp.IsSucceed = false
	}
	return resp, err
}

// DeleteTask 删除任务
func (s *OutboundTaskServer) DeleteTask(ctx context.Context, request *pb.DeleteTaskRequest) (*pb.DeleteTaskResponse, error) {
	resp, err := s.outboundTaskService.DeleteTask(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc DeleteTask request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc DeleteTask request:%+v, succeed", request)
	return resp, nil
}

// ExecuteTask 执行任务
func (s *OutboundTaskServer) ExecuteTask(ctx context.Context, request *pb.ExecuteTaskRequest) (*pb.ExecuteTaskResponse, error) {
	resp, err := s.outboundTaskService.ExecuteTask(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExecuteTask request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc ExecuteTask request:%+v, succeed", request)
	return resp, nil
}

func (s *OutboundTaskServer) Callback(ctx context.Context, request *pb.CallbackRequest) (*pb.CallbackResponse, error) {
	resp := &pb.CallbackResponse{}
	err := s.outboundTaskService.HandleCallback(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc Callback request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc Callback request:%+v, succeed", request)
	resp.IsSucceed = true
	return resp, nil
}

func (s *OutboundTaskServer) ImportCrowd(ctx context.Context, request *pb.ImportCrowdRequest) (*pb.ImportCrowdResponse, error) {
	resp, err := s.outboundTaskService.ImportCrowd(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ImportCrowd request:%+v, err:%s", request, err.Error())
		return nil, err
	}

	return resp, nil
}

func (s *OutboundTaskServer) GetOutboundClueList(ctx context.Context, request *pb.GetOutboundClueListRequest) (*pb.GetOutboundClueListResponse, error) {
	resp, err := s.outboundTaskService.GetClueList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetOutboundClueList request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil

}

func (s *OutboundTaskServer) GetOutboundClueDetail(ctx context.Context, request *pb.GetOutboundClueDetailRequest) (*pb.GetOutboundClueDetailResponse, error) {
	resp, err := s.outboundTaskService.GetClueDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetOutboundClueDetail request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

// GetTaskList 获取任务列表
func (s *OutboundTaskServer) GetTaskList(ctx context.Context, request *pb.GetTaskListRequest) (*pb.GetTaskListResponse, error) {
	resp, err := s.outboundTaskService.GetTaskList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetTaskList request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc GetTaskList request:%+v, succeed", request)
	return resp, nil
}

// GetTaskDetail 获取任务详情
func (s *OutboundTaskServer) GetTaskDetail(ctx context.Context, request *pb.GetTaskDetailRequest) (*pb.GetTaskDetailResponse, error) {
	resp, err := s.outboundTaskService.GetTaskDetail(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetTaskDetail request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc GetTaskDetail request:%+v, succeed", request)
	return resp, nil
}

// GetUnStartCallList 获取未开始的任务列表
func (s *OutboundTaskServer) GetUnStartCallList(ctx context.Context, request *pb.GetUnStartCallListRequest) (*pb.GetUnStartCallListResponse, error) {
	resp, err := s.outboundTaskService.GetUnStartCallList(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc GetUnStartCallList request:%+v, err:%s", request, err)
		return nil, err
	}
	stark.Logger.Infof(ctx, "grpc GetUnStartCallList request:%+v, succeed", request)
	return resp, nil
}

func (s *OutboundTaskServer) ExportTaskLog(ctx context.Context, request *pb.ExportTaskLogRequest) (*pb.ExportTaskLogResponse, error) {
	resp, err := s.outboundTaskService.ExportTaskLog(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc ExportTaskLog request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return resp, nil
}

func (s *OutboundTaskServer) AuditOutboundClue(ctx context.Context, request *pb.AuditOutboundClueRequest) (*pb.AuditOutboundClueResponse, error) {
	if request == nil {
		return nil, errors.New("request is nil")
	}
	if request.Intention != constants.IntentionA &&
		request.Intention != constants.IntentionB &&
		request.Intention != constants.IntentionC &&
		request.Intention != constants.IntentionD &&
		request.Intention != constants.IntentionE &&
		request.Intention != constants.IntentionF {
		return nil, errors.New("意向参数错误")
	}

	err := s.outboundTaskService.AuditOutboundClue(ctx, request)
	if err != nil {
		stark.Logger.Errorf(ctx, "grpc AuditOutboundClue request:%+v, err:%s", request, err.Error())
		return nil, err
	}
	return &pb.AuditOutboundClueResponse{IsSucceed: true}, nil
}
